package com.blog.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.blog.entity.Article;
import com.blog.entity.ArticleType;
import com.blog.entity.User;
import com.blog.service.IArticlePageViewService;
import com.blog.service.IArticleService;
import com.blog.service.IArticleTypeService;
import com.blog.vo.AjaxVo;
import com.blog.vo.ArticleVo;
import com.blog.vo.PageVo;

@Controller
@RequestMapping("/article")
public class ArticleController {

	@Autowired
	IArticleService articleService;

	@Autowired
	IArticleTypeService articleTypeService;
	
	@Autowired
	IArticlePageViewService articlePageViewService;
	/**
	 * 跳转到发表文章页面
	 * @return
	 */
	@GetMapping("/save")
	public ModelAndView saveUI() {
		List<ArticleType> articleTypeList = articleTypeService.findAll();
		ModelAndView mav = new ModelAndView("article/form.html");
		mav.addObject("typeList", articleTypeList);
		return mav;
	}
	
	/**
	 * 跳转到文章列表页面
	 * @param session
	 * @return
	 */
	@GetMapping("/list")
	public ModelAndView listArticle(String typeId,String title,Integer pageNum, Integer pageSize,HttpSession session) {
		//传递所有文章类型
		List<ArticleType> articleTypeList = articleTypeService.findAll();
		ModelAndView mav = new ModelAndView("article/list.html");
		mav.addObject("typeList", articleTypeList);
		
		//传递用户发表的文章
		String username="";
		User user = (User)session.getAttribute("user");
		if(user!=null){
			username=user.getUsername();
		}
		PageVo articleList=articleService.findPage(typeId,title,pageNum==null||pageNum<1?1:pageNum, 10,username);
		mav.addObject("articleList", articleList);
		return mav;
	}
	

	/**
	 * @Valid 400报错可能问题 BindingResult 参数顺序不对 <br/>
	 * @Valid ArticleVo articleVo, BindingResult result <br/>
	 *        TODO HttpSession 是否可以提出来<br/>
	 * @param articleVo
	 * @param session
	 * @param result
	 * @return
	 */
	/*@PostMapping("/save")
	public ModelAndView save(@Valid ArticleVo articleVo, BindingResult result, HttpSession session,
			RedirectAttributes redirectAttributes) {
		ModelAndView mav = new ModelAndView("redirect:/article/list");
		if (result.hasErrors()) {
			mav = new ModelAndView("redirect:/common/err");
			redirectAttributes.addFlashAttribute("errorList", result.getAllErrors());
			return mav;
		}
		Article article = new Article();
		article.setId(UUID.randomUUID().toString().replace("-", ""));
		article.setUserId(session.getAttribute("userId").toString());
		article.setInfo(articleVo.getInfo());
		article.setText(articleVo.getText());
		article.setTypeId(articleVo.getTypeId());
		article.setTitle(articleVo.getTitle());
		articleService.save(article);
		return mav;
	}*/

	/*
	 * @PostMapping("/save")
	 * 
	 * @ResponseBody public AjaxVo save(Article article){
	 * article.setId(UUID.randomUUID().toString().replace("-", ""));
	 * articleService.save(article); return new AjaxVo().setErrorCode("0"); }
	 */
	/**
	 * json 验证保存表单
	 * @param articleVo
	 * @param result
	 * @param session
	 * @return
	 * @throws InterruptedException
	 */
	@PostMapping("/save")
	@ResponseBody
	public AjaxVo save(@Valid ArticleVo articleVo, BindingResult result, HttpSession session)
			throws InterruptedException {
		// 模拟网络超时
		 //Thread.sleep(3000);

		if (result.hasErrors()) {
			for (ObjectError error : result.getAllErrors()) {
				return new AjaxVo().setErrorCode("1002").setErrorMsg(error.getDefaultMessage());
			}
		}
		
		//为发表的文章在数据库添加一条浏览量
		String id=UUID.randomUUID().toString().replace("-", "");
		articlePageViewService.save(id, 0);
		
		//往数据库保存发表的文章
		Article article = new Article();
		article.setId(UUID.randomUUID().toString().replace("-", ""));
		User user=(User)session.getAttribute("user");
		article.setUserId(user.getId());
		article.setInfo(articleVo.getInfo());
		article.setText(articleVo.getText());
		article.setTypeId(articleVo.getTypeId());
		article.setTitle(articleVo.getTitle());
		article.setPageViewId(id);
		articleService.save(article);
		
		
		
		return new AjaxVo().setErrorCode("0");
	}
	
}
